package com.example.diethealthsystem.controller;

import com.example.diethealthsystem.entity.HealthGoal;
import com.example.diethealthsystem.service.HealthGoalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/healthgoals")
public class HealthGoalController {

    private final HealthGoalService healthGoalService;

    @Autowired
    public HealthGoalController(HealthGoalService healthGoalService) {
        this.healthGoalService = healthGoalService;
    }

    @GetMapping
    public ResponseEntity<List<HealthGoal>> getAllHealthGoals() {
        List<HealthGoal> healthGoals = healthGoalService.findAll();
        return new ResponseEntity<>(healthGoals, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<HealthGoal> getHealthGoalById(@PathVariable Long id) {
        return healthGoalService.findById(id)
                .map(healthGoal -> new ResponseEntity<>(healthGoal, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PostMapping
    public ResponseEntity<HealthGoal> createHealthGoal(@RequestBody HealthGoal healthGoal) {
        HealthGoal createdHealthGoal = healthGoalService.save(healthGoal);
        return new ResponseEntity<>(createdHealthGoal, HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<HealthGoal> updateHealthGoal(@PathVariable Long id, @RequestBody HealthGoal healthGoal) {
        return healthGoalService.findById(id)
                .map(existingHealthGoal -> {
                    existingHealthGoal.setUser(healthGoal.getUser());
                    existingHealthGoal.setGoalType(healthGoal.getGoalType());
                    existingHealthGoal.setTargetValue(healthGoal.getTargetValue());
                    existingHealthGoal.setStartDate(healthGoal.getStartDate());
                    existingHealthGoal.setEndDate(healthGoal.getEndDate());
                    existingHealthGoal.setCurrentProgress(healthGoal.getCurrentProgress());
                    HealthGoal updatedHealthGoal = healthGoalService.save(existingHealthGoal);
                    return new ResponseEntity<>(updatedHealthGoal, HttpStatus.OK);
                })
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteHealthGoal(@PathVariable Long id) {
        if (healthGoalService.findById(id).isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        healthGoalService.deleteById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<HealthGoal>> getHealthGoalsByUserId(@PathVariable Long userId) {
        List<HealthGoal> healthGoals = healthGoalService.findByUserId(userId);
        if (healthGoals.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(healthGoals, HttpStatus.OK);
    }
} 